<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>全部帖子</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="lib/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="css/admin.css" media="all">
        
	</head>
	<body>
        <div class="layui-fluid">
			<div class="layui-col-md12">
				<div class="layui-navs">
					<span class="layui-breadcrumb" lay-separator="—">
						<a href="javascript:;">系统设置</a>
						<a href="javascript:;">管理员设置</a>
						<a><cite>置顶的帖子</cite></a>
					</span>
					<a class="layui-btn layui-btn-small" style="line-height:1.6em;float:right;height: 36px;" href="javascript:location.replace(location.href);" title="刷新">
						<i class="layui-icon layui-icon-refresh" style="line-height:36px;font-size: 16px;"></i>
					</a>
				</div>
			</div>
			<div class="layui-col-md12">
				<div class="layui-card">
					<div class="layui-card-body ">
						<form class="layui-form layui-col-space5" action="javascript:void(0);">
							<!--<div class="layui-inline layui-show-xs-block">-->
								<!--<input type="text" name="start_time" id="start_time" value="" placeholder="请选择开始时间" autocomplete="off" class="layui-input">-->
							<!--</div>-->
							<!--<div class="layui-inline layui-show-xs-block">-->
								<!--<input type="text" name="end_time" id="end_time" value="" placeholder="请选择结束时间" autocomplete="off" class="layui-input">-->
							<!--</div>-->
							<div class="layui-inline layui-show-xs-block">
								<input type="text" name="name" value="" id="input1"  placeholder="请输入想搜索的帖子" autocomplete="off" class="layui-input" style="width: 300px">
							</div>
							<div class="layui-inline layui-show-xs-block">
								<button class="layui-btn"  lay-submit="javascript:void(0);" lay-filter="javascript:void(0);" onclick="serach()"><i class="layui-icon">&#xe615;</i></button>
							</div>
						</form>
					</div>
					<script>
						// 模糊根据帖子名搜索的实现
						function serach() {
							var info = $('#input1').val()
							$.ajax({
								url:"/forum/post/selectByPostName?postName=" + info,
								type:"post",
								dataType:"json",
								success:function (data) {
									var html = "";
									for (var i = 0; i < data.length; i++){
										html = html + "<tr><td>" + (i+1);
										html = html + "</td><td>" + data[i].postName;
										html = html + "</td><td>" + data[i].userName;
										html = html + "</td><td>" + data[i].postType;
										html = html + "</td><td>" + data[i].postTime;
										html = html + "</td><td><a class=\"layui-btn layui-btn-sm layui-btn-normal\" title=\"详情\" onclick=\"execute_open('编辑角色', 'role_operation.html?id=" + data[i].postId + "', 800, 500)\" href=\"javascript:;\"><i class=\"layui-icon layui-icon-edit\"></i>详情</a>"
										if (data[i].status == 1){
											html = html + "<a class=\"layui-btn layui-btn-sm layui-btn-danger\" title=\"取消\" onclick=\"stick1(" + data[i].postId + ")\" href=\"javascript:;\"><i class=\"layui-icon layui-icon-delete\"></i>取消置顶</a>"
										} else if (data[i].status == 0){
											html = html + "<a class=\"layui-btn layui-btn-sm layui-btn-normal\" title=\"置顶\" onclick=\"stick0(" + data[i].postId + ")\" href=\"javascript:;\"><i class=\"layui-icon layui-icon-edit\"></i>置顶该帖</a>"
										}
										html = html + "</td></tr>";
									}
									console.log(html)
									$('#tbody').html(html)
								}
							})
						}
					</script>
					<!--<div class="layui-card-header">-->
						<!--<button class="layui-btn" onclick="execute_open('添加角色', 'role_operation.html', '1000', '800')"><i class="layui-icon"></i>添加角色</button>-->
					<!--</div>-->
					<div class="layui-card-header">
					<button class="layui-btn" onclick="time()"><i class="layui-icon"></i>最近时间</button>
					</div>
					<div class="layui-card-body">
						<table class="layui-table layui-form">
							<thead>
								<tr>
									<th>序号</th>
									<th>帖子名</th>
									<th>用户名</th>
									<th>帖子类型</th>
									<th>发帖时间</th>
									<th>管理</th>
								</tr>
							</thead>
							<tbody id="tbody">
								<!--<tr>-->
									<!--<td>1</td>-->
									<!--<td>1</td>-->
									<!--<td>超级管理员</td>-->
									<!--<td>10</td>-->
									<!--<td>3</td>-->
									<!--<td>-->
										<!--<a class="layui-btn layui-btn-sm layui-btn-normal" title="编辑" onclick="execute_open('编辑角色', 'role_operation.html?id=1', 1000, 800)" href="javascript:;"><i class="layui-icon layui-icon-edit"></i>详情</a>-->
										<!--<a class="layui-btn layui-btn-sm layui-btn-danger" title="删除" onclick="execute_del(this, 1, '')" href="javascript:;"><i class="layui-icon layui-icon-delete"></i>删除</a>-->
									<!--</td>-->
								<!--</tr>-->
							</tbody>
						</table>
					</div>
					<div class="page" style="text-align: center">
						<ul class="pagination">
							<!--<li class="disabled"><span>上一页</span></li> -->
							<!--<li class="active"><span>1</span></li>-->
							<!--<li><a href="javascript:;">2</a></li>-->
							<!--<li><a href="javascript:;">3</a></li>-->
							<!--<li><a href="javascript:;">4</a></li> -->
							<!--<li><a href="javascript:;">下一页</a></li>-->
						</ul>
					</div>
				</div>
			</div>
        </div>

    </body>


	<script src="../jquery/jquery-3.4.1.js"></script>
	<script>
		$.ajax({
			url:"/forum/post/selectAll",
			type:"post",
			dataType:"json",
			success:function (data) {
				var html = "";
				for (var i = 0; i < data.length; i++){
					html = html + "<tr><td>" + (i+1);
					html = html + "</td><td>" + data[i].postName;
					html = html + "</td><td>" + data[i].userName;
					html = html + "</td><td>" + data[i].postType;
					html = html + "</td><td>" + data[i].postTime;
					html = html + "</td><td id='" + data[i].postId +"'><a class=\"layui-btn layui-btn-sm layui-btn-normal\" title=\"详情\" onclick=\"execute_open('编辑角色', 'role_operation.html?id=" + data[i].postId + "', 800, 500)\" href=\"javascript:;\"><i class=\"layui-icon layui-icon-edit\"></i>详情</a>";
					if (data[i].status == 1){
						html = html + "<a class=\"layui-btn layui-btn-sm layui-btn-danger\" title=\"取消\" onclick=\"stick1(" + data[i].postId + ")\" href=\"javascript:;\"><i class=\"layui-icon layui-icon-delete\"></i>取消置顶</a>"
					} else if (data[i].status == 0){
						html = html + "<a class=\"layui-btn layui-btn-sm layui-btn-normal\" title=\"置顶\" onclick=\"stick0(" + data[i].postId + ")\" href=\"javascript:;\"><i class=\"layui-icon layui-icon-edit\"></i>置顶该帖</a>"
					}
					html = html + "</td></tr>";
				}
				console.log(html)
				$('#tbody').html(html)
			}
		})


		function stick0(info) {
			var html2 = "<a class=\"layui-btn layui-btn-sm layui-btn-normal\" title=\"详情\" onclick=\"execute_open('编辑角色', 'role_operation.html?id=" + info + "', 800, 500)\" href=\"javascript:;\"><i class=\"layui-icon layui-icon-edit\"></i>详情</a>";
			var html2 = html2 + "<a class=\"layui-btn layui-btn-sm layui-btn-danger\" title=\"取消\" onclick=\"stick1(" + info + ")\" href=\"javascript:;\"><i class=\"layui-icon layui-icon-delete\"></i>取消置顶</a>";
            // var role = $('input[name="role"]:checked').val()
            // // 如果使用的是表单，直接进行表单序列化即可
            // var parmeter = "user=" + user + "&pwd=" + pwd + "&role=" + role
			var parmeter = "postId=" + info;
			// 先判断是否已经置顶超过规定的个数
			$.ajax({
				url:"/forum/post/selectByStatus",
				success:function (data) {
					if (data > 4){
						alert("置顶帖上限为5个，请先取消部分")
						return false;
					} else {
						$.ajax({
							url:"/forum/post/updateStatusToOne",
							data:parmeter,
							type:"post",
							success:function (result) {
								if (result == "success"){
									// $('#td')
									// alert(result)
									// $(this).val("123")
									$("#" + info).html(html2);
									// location.replace(location.href);
								} else {
									alert("操作失败，请稍后重试")
								}
							}
						})
					}
				}
			})

		}
	</script>
	<script>
		function stick1(info) {
			var html1 = "<a class=\"layui-btn layui-btn-sm layui-btn-normal\" title=\"详情\" onclick=\"execute_open('编辑角色', 'role_operation.html?id=" + info + "', 800, 500)\" href=\"javascript:;\"><i class=\"layui-icon layui-icon-edit\"></i>详情</a>";
			var html1 = html1 + "<a class=\"layui-btn layui-btn-sm layui-btn-normal\" title=\"置顶\" onclick=\"stick0(" + info + ")\" href=\"javascript:;\"><i class=\"layui-icon layui-icon-edit\"></i>置顶该帖</a>";
			var parmeter = "postId=" + info;
			// 先判断是否已经置顶超过规定的个数
			$.ajax({
				url:"/forum/post/selectByStatus",
				success:function (data) {
					if (data < 2){
						alert("置顶帖下限为1个，请先置顶其它再来取消")
						return false;
					} else {
						$.ajax({
							url:"/forum/post/updateStatusToZero",
							data:parmeter,
							type:"post",
							success:function (result) {
								if (result == "success"){
									// $('#td')
									// location.replace(location.href);
									// $(this).html(html1)
									$("#" + info).html(html1);
								} else {
									alert("操作失败，请稍后重试")
								}
							}
						})
					}
				}
			})
		}
	</script>
	<script>
		// 根据时间进行排序
		function time() {
			var a = $('td')
			alert(a)
			var test = a[0]
			alert(test)
		}
	</script>
</html>

<script src="js/jquery-3.4.1.min.js"></script>
<script src="lib/layui/layui.js"></script>
<script src="js/admin.js"></script>